package com.eascs.afterloan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.afterloan.dao.RefundConCodeDao;
import com.eascs.gaczcontract.entity.O2oLoanapply;
import com.eascs.workflow.common.service.sequence.SequenceService;

@Service
public class RefundConCodeService {

	@Reference
	private SequenceService sequenceService;
	
	@Autowired
	RefundConCodeDao refundDao;
	
	//生成带坏款款合同编号
	public String generateRefundCode(String billCode){
		String refundCode = "";
		//根据bill_code查询主表信息
		O2oLoanapply entity = refundDao.findLoanapplyByCode(billCode);
		// 获取编号
		String createCode = sequenceService.getSeq(billCode, 2);
		String splitCode[] = createCode.split("-");
		//根据bill_code查询放款银行
		String bank = refundDao.findBankByCode(billCode);
		if(entity.getInstrumentNumber()==null){
			if(bank==null){
				//如果放款银行为null，则等于entity.getBankType()
				bank = entity.getBankType();
			}
			refundCode = bank + "-确-" + entity.getBillCode() + "-" + splitCode[1];
		}else{
			//判断是否包含某字段（判断某字段的位置是否在首位存在）
			if(entity.getInstrumentNumber().indexOf("国安-")==0){
				StringBuffer str = new StringBuffer(entity.getInstrumentNumber());
				//替换字符
				refundCode = str.replace(3, 4, "确").append("-" + splitCode[1]).toString();
			}else if(entity.getInstrumentNumber().indexOf("EA-")==0){
				//插入字符
				StringBuffer str = new StringBuffer(entity.getInstrumentNumber());
				refundCode = str.insert(3, "确-").append("-" + splitCode[1]).toString();
			}else{
				if(bank==null){
					//如果放款银行为null，则等于entity.getBankType()
					bank = entity.getBankType();
				}
				StringBuffer str = new StringBuffer(entity.getInstrumentNumber());
				//在首位插入字符
				refundCode = str.insert(0, bank + "-确-").append("-" + splitCode[1]).toString();
			}
		}
		return refundCode;
	}
	
}
